#include "SpriteSelectorPanel.hpp"

SpriteSelectorPanel::SpriteSelectorPanel(int width, int height, SpriteManager* manager):Panel(0,0, width, height, Panel::VerticalStretch | Panel::HorizontalStretch){
    spriteManager = manager;
    background = new sf::RectangleShape(sf::Vector2f(rect.width, rect.height));
    sf::Color bgColor = sf::Color::Black;
    bgColor.a = 205;
    background->setFillColor(bgColor);
    add(background);

    spriteButtons = new IconButton*[spriteManager->spriteCount];
    for(int i = 0; i < spriteManager->spriteCount; i++){
        spriteButtons[i] = new IconButton(0,0,32,32, spriteManager->getSprite(i));
        add(spriteButtons[i]);
    }
    updateLayout(width, height);
}

void SpriteSelectorPanel::updateLayout(int width, int height){
    int columns = width/37;
    int y = 5;
    for(int i = 0; i < spriteManager->spriteCount; i++){
        spriteButtons[i]->rect.left = 5 + (i%columns)*37;
        if(i%columns == 0 && i != 0){
            y += 37;
        }
        spriteButtons[i]->rect.top = y;
    }
}

void SpriteSelectorPanel::onResize(int width, int height){
    Panel::onResize(width, height);
    background->setSize(sf::Vector2f(rect.width, rect.height));
    updateLayout(width, height);
}